package a66weding.com.jiehuntong.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.util.Log;

import org.greenrobot.eventbus.EventBus;

import a66weding.com.jiehuntong.beans.MessageBean;
import a66weding.com.jiehuntong.common.UpdateAppController;
import a66weding.com.jiehuntong.common.UpdateConstants;
import a66weding.com.jiehuntong.http.NetStatusUtil;


/**
 * Created by jh on 2016/7/13.
 */
public class NetStateChangeReceiver extends BroadcastReceiver {
    //防止多次调用
    private boolean states = false;

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
            boolean netConnected = NetStatusUtil.isNetworkAvailable(context);
            if (netConnected) {
                NetStatusUtil.NetState netState = NetStatusUtil.isConnected(context);

              if(netState.equals(NetStatusUtil.NetState.NET_WIFI)){ //wifi 连接
                  states=true;
              }else{//流量 连接
                  states = false;
              }

                if (states == true) {
                    states = false;
                    UpdateAppController.startUpdateServiceForDownContinue(context, UpdateConstants.RECEIVER_FLAG, UpdateConstants.RECEIVER_TEXT);
                }
                EventBus.getDefault().post(new MessageBean(UpdateConstants.ISCONNECT));

            } else {
                states = true;
                EventBus.getDefault().post(new MessageBean(UpdateConstants.DISCONNECT));
            }
        }
    }
}
